﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKRGeometry;
using SectionSignAndFilled.StairsRoomPlanSign.ControlLayer;

namespace SectionSignAndFilled.StairsRoomPlanSign.ModelLayer
{
	public class SignUseStairsPacker
	{
		public StairPlanDataPacker UseStairPlanData
		{
			get
			{
				return this.m_useStairPlanData;
			}
			private set
			{
				this.m_useStairPlanData = value;
			}
		}

		public ViewData UseViewData
		{
			get
			{
				return this.m_useViewData;
			}
			private set
			{
				this.m_useViewData = value;
			}
		}

		public List<Stairs> LstStairsForThisPacker
		{
			get
			{
				return this.m_lstStairsForThisPacker;
			}
			private set
			{
				this.m_lstStairsForThisPacker = value;
			}
		}

		public StairsKindEnum ThisStairsKind
		{
			get
			{
				return this.m_thisStairsKind;
			}
			private set
			{
				this.m_thisStairsKind = value;
			}
		}

		public List<StairsRunPacker> LstAllStiarsRunPacker
		{
			get
			{
				return this.m_lstAllStairsRunPacker;
			}
			private set
			{
				this.m_lstAllStairsRunPacker = value;
			}
		}

		public List<FlatRoofPacker> LstAllFlatRoofPacker
		{
			get
			{
				return this.m_lstAllFlatRoofPacker;
			}
			private set
			{
				this.m_lstAllFlatRoofPacker = value;
			}
		}

		public bool IfGetStairesKindRight
		{
			get
			{
				return this.m_bIfGetStairesKindRight;
			}
			private set
			{
				this.m_bIfGetStairesKindRight = value;
			}
		}

		public double NowBaseElevation
		{
			get
			{
				return this.m_dNowBaseElevation;
			}
			private set
			{
				this.m_dNowBaseElevation = value;
			}
		}

		public StairConditionEnum ThisStairsCondition
		{
			get
			{
				return this.m_thisStairsCondition;
			}
			private set
			{
				this.m_thisStairsCondition = value;
			}
		}

		public List<SameLevelStairsPacker> LstAllSameLevelStairsPacker
		{
			get
			{
				return this.m_lstAllSameLevelStairsPacker;
			}
			private set
			{
				this.m_lstAllSameLevelStairsPacker = value;
			}
		}

		public SignUseStairsPacker(StairPlanDataPacker inputStairPlanData, List<Stairs> lstInputStairs)
		{
			this.UseStairPlanData = inputStairPlanData;
			this.UseViewData = this.UseStairPlanData.UseViewData;
			this.LstStairsForThisPacker = new List<Stairs>();
			this.LstStairsForThisPacker.AddRange(lstInputStairs);
			this.ThisStairsCondition = this.UseStairPlanData.StairConditon;
			this.PrepareMethod();
		}

		public SignUseStairsPacker(StairPlanDataPacker inputStairPlanData, List<SameLevelStairsPacker> lstInputStairs)
		{
			this.UseStairPlanData = inputStairPlanData;
			this.UseViewData = this.UseStairPlanData.UseViewData;
			this.LstAllSameLevelStairsPacker = new List<SameLevelStairsPacker>();
			this.LstAllSameLevelStairsPacker.AddRange(lstInputStairs);
			this.ThisStairsCondition = this.UseStairPlanData.StairConditon;
			this.PrepareMethod();
		}

		private void PrepareMethod()
		{
			this.PrepareCompoent();
			this.PreparePackers();
			this.NowBaseElevation = this.GetNowStairsBaseElevation();
			StairsKindEnum thisStairsKind;
			this.IfGetStairesKindRight = this.m_nowUseKindDetctor.TryDetectStairsKind(this, out thisStairsKind);
			if (this.IfGetStairesKindRight)
			{
				this.ThisStairsKind = thisStairsKind;
			}
		}

		private void PreparePackers()
		{
			List<StairsRunPacker> collection = new List<StairsRunPacker>();
			List<FlatRoofPacker> collection2 = new List<FlatRoofPacker>();
			if (this.TryGetAllStairsRunPacker(out collection))
			{
				this.LstAllStiarsRunPacker.AddRange(collection);
			}
			if (this.TryGetAllFlatRoofPacker(out collection2))
			{
				this.LstAllFlatRoofPacker.AddRange(collection2);
			}
			foreach (StairsRunPacker stairsRunPacker in this.LstAllStiarsRunPacker)
			{
				stairsRunPacker.UpdataThisShapeByView(this.UseViewData);
			}
			foreach (FlatRoofPacker flatRoofPacker in this.LstAllFlatRoofPacker)
			{
				flatRoofPacker.UpdataThisShapeByView(this.UseViewData);
			}
			foreach (StairsRunPacker inputRun in this.LstAllStiarsRunPacker)
			{
				foreach (FlatRoofPacker flatRoofPacker2 in this.LstAllFlatRoofPacker)
				{
					flatRoofPacker2.IfRunLinkThis(inputRun);
				}
			}
		}

		private void PrepareCompoent()
		{
			this.m_lstAllStairsRun = new List<StairsRun>();
			this.m_lstAllStairsLanding = new List<StairsLanding>();
			foreach (Stairs stairs in this.LstStairsForThisPacker)
			{
				foreach (ElementId elementId in stairs.GetStairsRuns())
				{
					if (stairs.Document.GetElement(elementId) is StairsRun)
					{
						this.m_lstAllStairsRun.Add(stairs.Document.GetElement(elementId) as StairsRun);
					}
				}
				foreach (ElementId elementId2 in stairs.GetStairsLandings())
				{
					if (stairs.Document.GetElement(elementId2) is StairsLanding)
					{
						this.m_lstAllStairsLanding.Add(stairs.Document.GetElement(elementId2) as StairsLanding);
					}
				}
			}
		}

		private double GetNowStairsBaseElevation()
		{
			double result = 0.0;
			StairsRunPacker stairsRunPacker = null;
			foreach (StairsRunPacker stairsRunPacker2 in this.LstAllStiarsRunPacker)
			{
				if (stairsRunPacker == null || Geometry.LessThan(stairsRunPacker.EndElevation, stairsRunPacker2.EndElevation, CmdStairsRoomPlanSign.NearTolerance))
				{
					stairsRunPacker = stairsRunPacker2;
					result = stairsRunPacker.StartElevation;
				}
			}
			return result;
		}

		private bool TryGetAllStairsRunPacker(out List<StairsRunPacker> allStairsRunPacker)
		{
			allStairsRunPacker = new List<StairsRunPacker>();
			foreach (StairsRun inputStairsRun in this.m_lstAllStairsRun)
			{
				allStairsRunPacker.Add(new StairsRunPacker(inputStairsRun, this.UseStairPlanData, null));
			}
			foreach (SameLevelStairsPacker sameLevelStairsPacker in this.LstAllSameLevelStairsPacker)
			{
				foreach (KeyValuePair<StairsRun, RevitLinkInstance> keyValuePair in sameLevelStairsPacker.GetLstStairsRunWithRevitLinkInstance())
				{
					allStairsRunPacker.Add(new StairsRunPacker(keyValuePair.Key, this.UseStairPlanData, keyValuePair.Value));
				}
			}
			return true;
		}

		private bool TryGetAllFlatRoofPacker(out List<FlatRoofPacker> allFlatRoofPacker)
		{
			allFlatRoofPacker = new List<FlatRoofPacker>();
			foreach (StairsLanding input in this.m_lstAllStairsLanding)
			{
				allFlatRoofPacker.Add(new FlatRoofPacker(input, null));
			}
			foreach (SameLevelStairsPacker sameLevelStairsPacker in this.LstAllSameLevelStairsPacker)
			{
				foreach (KeyValuePair<StairsLanding, RevitLinkInstance> keyValuePair in sameLevelStairsPacker.GetLstStairsLandingWithRevitLinkInstance())
				{
					allFlatRoofPacker.Add(new FlatRoofPacker(keyValuePair.Key, keyValuePair.Value));
				}
			}
			return true;
		}

		private StairPlanDataPacker m_useStairPlanData;

		private ViewData m_useViewData;

		private List<Stairs> m_lstStairsForThisPacker = new List<Stairs>();

		private StairsKindEnum m_thisStairsKind;

		private StairConditionEnum m_thisStairsCondition;

		private List<StairsRun> m_lstAllStairsRun = new List<StairsRun>();

		private List<StairsLanding> m_lstAllStairsLanding = new List<StairsLanding>();

		private List<StairsRunPacker> m_lstAllStairsRunPacker = new List<StairsRunPacker>();

		private List<StairsWellUnitPacker> m_lstAllStairsWellUnitPacker = new List<StairsWellUnitPacker>();

		private List<FlatRoofPacker> m_lstAllFlatRoofPacker = new List<FlatRoofPacker>();

		private bool m_bIfGetStairesKindRight;

		private double m_dNowBaseElevation;

		private StairsKindDetector m_nowUseKindDetctor = StairsKindDetector.GetDetector();

		private List<SameLevelStairsPacker> m_lstAllSameLevelStairsPacker = new List<SameLevelStairsPacker>();
	}
}
